package com.xiaoxie.boot.controller;


import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.*;
import com.xiaoxie.boot.codeutil.IVerifyCodeGen;
import com.xiaoxie.boot.codeutil.SimpleCharVerifyCodeGenImpl;
import com.xiaoxie.boot.codeutil.VerifyCode;
import com.xiaoxie.boot.service.*;
import com.xiaoxie.boot.utils.DataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxie
 * @create 2022-02-12 19:55
 */
@ApiIgnore
@Controller
@Slf4j
public class UtilController {
    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    @Autowired
    private BookTypeService bookTypeService;

    @Autowired
    private BorrowBookService borrowBookService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UtilService utilService;

    /**
     * 首页统计和图标数据查询
     *
     * @return
     */
    @RequestMapping("/getCount")
    @ApiOperation(value = "首页统计和图标数据查询", notes = "首页统计和图标数据查询")
    @ApiOperationSupport(author = "小谢")
    public Map<String, Object> getCount() {
        Map<String, Object> map = new HashMap<>(16);
        int user = userService.selectUserNumber();
        int book = bookService.selectBookNumber();
        int bookType = bookTypeService.selectAllBookTypeNumber();
        int borrowBook = borrowBookService.selectAllBorrowBookNumber();
        map.put("user", user);
        map.put("book", book);
        map.put("bookType", bookType);
        map.put("borrowBook", borrowBook);
        return map;
    }

    /**
     * 查询公告信息
     *
     * @return
     */
    @RequestMapping("/queryNotice")
    public String welcome(Model model) {
        //查询公告信息
        PageInfo<Notice> noticePageInfo = noticeService.selectAllNotice(null, 1, 6);
        List<Notice> noticeList = noticePageInfo.getList();
        model.addAttribute("noticeList", noticeList);
        return "page/welcome";
    }

    /**
     * 获取验证码方法
     *
     * @param request
     * @param response
     */
    @RequestMapping("/verifyCode")
    @ApiOperation(value = "获取验证码", notes = "获取验证码方法")
    @ApiOperationSupport(author = "小谢")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            System.out.println("异常处理");
        }
    }

    /**
     * 根据图书Id查出数据跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/bookEdit")
    public String bookEdit(Integer id, Model model) {
        Book book = bookService.selectByPrimaryKey(id);
        model.addAttribute("book", book);
        return "page/book/bookEdit";
    }

    /**
     * 根据图书类型Id查出数据跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/bookTypeEdit")
    public String bookTypeEdit(Integer id, Model model) {
        BookType bookType = bookTypeService.selectByPrimaryKey(id);
        model.addAttribute("bookType", bookType);
        return "page/bookType/bookTypeEdit";
    }

    /**
     * 根据用户Id查出数据跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/readerEdit")
    public String readerEdit(Integer id, Model model) {
        User user = userService.selectByPrimaryKey(id);
        model.addAttribute("user", user);
        return "page/reader/readerEdit";
    }

    /**
     * 根据公告Id查出数据跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/noticeEdit")
    public String noticeEdit(Integer id, Model model) {
        Notice notice = noticeService.selectByPrimaryKey(id);
        model.addAttribute("notice", notice);
        return "page/notice/noticeEdit";
    }

    /**
     * 根据异常还书id查询异常还书信息并跳转异常还书界面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toAbnormalBorrowBook")
    public String toAbnormalBorrowBook(@RequestParam("id") Integer id, Model model) {
        BorrowBook borrowBook = borrowBookService.selectByPrimaryKey(id);
        model.addAttribute("borrowBook", borrowBook);
        return "page/borrowBook/abnormalBorrowBook";
    }

    /**
     * 借阅时间线
     */
    @RequestMapping("/queryBookList")
    public String queryBookList(String flag, Integer id, Model model) {
        List<BorrowBook> list = null;
        if ("book".equals(flag)) {
            list = borrowBookService.queryBorrowBookList(id, null);
        } else {
            list = borrowBookService.queryBorrowBookList(null, id);
        }
        model.addAttribute("borrowBookList", list);
        System.out.println(list);
        return "page/borrowBook/bookTime";
    }

    /**
     * 根据角色id查询异常还书信息并跳转异常还书界面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toRoleEdit")
    public String toRoleEdit(@RequestParam("id") Integer id, Model model) {
        Role role = roleService.selectByPrimaryKey(id);
        model.addAttribute("Role", role);
        return "page/role/roleEdit";
    }

    /**
     * 根据管理员Id查出数据跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/adminEdit")
    public String adminEdit(Integer id, Model model) {
        User user = userService.selectByPrimaryKey(id);
        model.addAttribute("admin", user);
        return "page/admin/adminEdit";
    }

    /**
     * 查询角色拥有的菜单角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryRoleMenu")
    @ResponseBody
    @ApiOperation(value = "查询角色拥有的菜单角色", notes = "查询角色拥有的菜单角色")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryRoleMenu(Integer id) {
        List<Integer> integers = utilService.selectRoleMenu(id);
        return DataInfo.ok("成功", integers);
    }

    /**
     * 删除角色
     *
     * @param id
     * @param ids
     * @return
     */
    @RequestMapping("/RoleMenuSave")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除角色")
    @ApiOperationSupport(author = "小谢")
    public DataInfo RoleMenu(Integer id, String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        int i = utilService.deleteByRoleId(id);
        log.info("删除了：" + i + "条记录");
        for (int j = 0; j < list.size(); j++) {
            utilService.addRoleMenu(id, Integer.valueOf(list.get(j)));
        }
        return DataInfo.ok();
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @RequestMapping("/queryAllRole")
    @ResponseBody
    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllRole() {
        List<Role> roles = utilService.selectAllRole();
        return DataInfo.ok("成功", roles.size(), roles);
    }

    /**
     * 用户角色绑定
     *
     * @param uid
     * @param rid
     * @return
     */
    @RequestMapping("/userRoleSave")
    @ResponseBody
    @ApiOperation(value = "用户角色绑定", notes = "用户角色绑定")
    @ApiOperationSupport(author = "小谢")
    public DataInfo userRoleSave(@RequestParam("uid") Integer uid, @RequestParam("rid") Integer rid) {
        int a = utilService.deleteByUserId(uid);
        int b = utilService.addUserRole(uid, rid);
        if (b == 1) {
            return DataInfo.ok();
        } else {
            return DataInfo.fail("绑定失败");
        }
    }
}
